package com.reguang.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.reguang.bo.SysUserBO;
import com.reguang.mapper.SysUserMapper;
import com.reguang.mapper.SysUserRoleMapper;
import com.reguang.pojo.SysUserDO;
import com.reguang.pojo.SysUserRoleDO;
import com.reguang.service.SysUserService;
import com.reguang.utils.ObjectUtils;

import tk.mybatis.mapper.entity.Example;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	@Override
	public SysUserDO queryOne(SysUserDO record) {
		return sysUserMapper.queryOne(record);
	}

	@Override
	public PageInfo<SysUserDO> list(Map<String, Object> params) {
		// 设置分页
		PageHelper.startPage(Integer.parseInt(params.get("offset").toString()),
				Integer.parseInt(params.get("limit").toString()));
		Example example = new Example(SysUserDO.class);
		example.createCriteria().andLike("name", "%" + params.get("name") + "%")//
				.andLike("deptId", "%" + params.get("deptId") + "%");
		List<SysUserDO> list = sysUserMapper.selectByExample(example);
		return new PageInfo<SysUserDO>(list);
	}

	@Override
	public SysUserBO queryByPrimaryKey(String id) {
		return sysUserMapper.queryByPrimaryKey(id);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer save(SysUserBO user) {
		SysUserDO sysUser = new SysUserDO();
		BeanUtils.copyProperties(user, sysUser);
		String userId = Sid.nextShort();
		sysUser.setId(userId);
		sysUser.setCreateTime(new Date());
		Integer result = sysUserMapper.insert(sysUser);
		List<String> roles = user.getRoleIds();
		List<SysUserRoleDO> list = new ArrayList<>();
		for (String roleId : roles) {
			SysUserRoleDO ur = new SysUserRoleDO();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (ObjectUtils.isNotEmpty(list)) {
			sysUserRoleMapper.insertList(list);
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer update(SysUserBO user) {
		SysUserDO sysUser = new SysUserDO();
		BeanUtils.copyProperties(user, sysUser);
		sysUser.setUpdateTime(new Date());
		Integer result = sysUserMapper.updateByPrimaryKeySelective(sysUser);
		String userId = sysUser.getId();
		List<String> roles = user.getRoleIds();

		List<SysUserRoleDO> list = new ArrayList<>();
		for (String roleId : roles) {
			SysUserRoleDO ur = new SysUserRoleDO();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (ObjectUtils.isNotEmpty(list)) {
			// 清除关联关系
			Example example = new Example(SysUserRoleDO.class);
			example.createCriteria().andEqualTo("userId", userId);
			sysUserRoleMapper.deleteByExample(example);
			sysUserRoleMapper.insertList(list);
		}
		return result;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer deleteById(String id) {
		return sysUserMapper.deleteByPrimaryKey(id);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer batchRemove(String[] userIds) {
		Example example = new Example(SysUserDO.class);
		List<String> ids = Arrays.asList(userIds);
		example.createCriteria().andIn("id", ids);
		return sysUserMapper.deleteByExample(example);
	}

	@Override
	public Integer resetPwd(SysUserDO user) {
		Integer result = sysUserMapper.updateByPrimaryKeySelective(user);
		return result;
	}

}
